package com.xiang.reflection;

import java.lang.annotation.*;
import java.lang.reflect.Field;

public class test11 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class c1 = Class.forName("com.xiang.reflection.Student1");

        //通过反射获得注解
        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }

        //获取value的值
        TableXiang tableXiang = (TableXiang)c1.getAnnotation(TableXiang.class);
        String value = tableXiang.TableName();
        System.out.println(value);

        //获得类指定的注解
        Field name = c1.getDeclaredField("name");
        FieldXiang annotation = name.getAnnotation(FieldXiang.class);
        System.out.println(annotation.columnName());
    }
}

//实体类 pojo .entity
@TableXiang(TableName = "xiangmou")
class Student1{

    @FieldXiang(columnName = "db_name",type = "varchar",length = 4)
    private String name;
    @FieldXiang(columnName = "db_id",type = "int",length = 10)
    private int id;
    @FieldXiang(columnName = "db_age",type = "int",length = 10)
    private int age;

    public Student1(String name, int id, int age) {
        this.name = name;
        this.id = id;
        this.age = age;
    }

    public Student1() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student1{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", age=" + age +
                '}';
    }
}

//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableXiang{
    String TableName();
}

//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldXiang{
    String columnName();
    String type();
    int length();
}